
package com.insigma.business.qrcode.intell._provider.impl;

import cn.hutool.core.bean.BeanUtil;
import com.insigma.business.qrcode.common._provider.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.SessionUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.intell._provider.CommonPolicyBFFProvider;
import com.insigma.business.qrcode.intell._provider.CommonPolicyProvider;
import com.insigma.business.qrcode.intell.dto.CommonPolicyDTO;
import com.insigma.business.qrcode.intell.dto.CommonPolicyInsertRequest;
import com.insigma.business.qrcode.intell.dto.CommonPolicySearchRequest;
import com.insigma.business.qrcode.intell.dto.CommonPolicyUpdateRequest;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("commonPolicyBFFProvider")
public class CommonPolicyBFFProviderImpl extends BaseProviderImpl implements CommonPolicyBFFProvider {

    @Autowired
    private CommonPolicyProvider commonPolicyProvider;


    @Override
    public BasePagingResponse<List<CommonPolicyDTO>> searchCommonPolicy(final CommonPolicySearchRequest searchRequest) {

        final BasePagingResponse<List<CommonPolicyDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CommonPolicy的请求参数不能为空");
            }

            @Override
            public void executeService() {
                BasePagingResponse<List<CommonPolicyDTO>> listBasePagingResponse = commonPolicyProvider.searchCommonPolicy(searchRequest);
                BeanUtil.copyProperties(listBasePagingResponse, basePagingResponse);
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_POLICY_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<CommonPolicyDTO> getCommonPolicy(final BaseGetRequest baseGetRequest){

        final BaseResponse<CommonPolicyDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CommonPolicy的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final BaseResponse<CommonPolicyDTO> commonPolicyResponse = commonPolicyProvider.getCommonPolicy(baseGetRequest);
                BeanUtil.copyProperties(commonPolicyResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_POLICY_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCommonPolicy(final CommonPolicyInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CommonPolicy的请求参数不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildCreatorOfGov(insertRequest);
                BaseResponse<String> commonPolicyResponse = commonPolicyProvider.insertCommonPolicy(insertRequest);
                BeanUtil.copyProperties(commonPolicyResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_POLICY_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateCommonPolicy(final CommonPolicyUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CommonPolicy的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildModifierOfGov(updateRequest);
                BaseResponse<Integer> commonPolicyResponse = commonPolicyProvider.updateCommonPolicy(updateRequest);
                BeanUtil.copyProperties(commonPolicyResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_POLICY_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> publishCommonPolicy(final CommonPolicyUpdateRequest updateRequest){

        SessionUtils.buildModifierOfGov(updateRequest);
        return commonPolicyProvider.publishCommonPolicy(updateRequest);
    }


    @Override
    public BaseResponse<Integer> topCommonPolicy(final CommonPolicyUpdateRequest updateRequest){

        SessionUtils.buildModifierOfGov(updateRequest);
        return commonPolicyProvider.topCommonPolicy(updateRequest);
    }


    @Override
    public BaseResponse<Integer> deleteCommonPolicy(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CommonPolicy的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildDeleteOfGov(deleteRequest);
                BaseResponse<Integer> commonPolicyResponse = commonPolicyProvider.deleteCommonPolicy(deleteRequest);
                BeanUtil.copyProperties(commonPolicyResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_POLICY_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }
}
